package com.ddwl.goods.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.goods.dao.mapper.SkuMapper;
import com.ddwl.goods.dao.model.Sku;
import com.ddwl.schema.bo.goods.ItemListApiBo;
import com.ddwl.schema.bo.goods.SkuBo;
import com.ddwl.schema.bo.order.SkuStockBo;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.Collection;
import java.util.List;

/**
 * 类SkuDao.java的实现描述：skuDao
 *
 * @author zach
 * @Date :2020-06-08 16:36.
 */
@Repository
public class SkuDao extends TkDao<SkuMapper, Sku> {

    @Resource
    private SkuMapper skuMapper;

    public List<Sku> findByItemId(String itemId, Integer skuStatus) {
        Example example = getExample();
        example.and().andEqualTo("itemId", itemId)
                .andEqualTo("skuStatus", skuStatus);
        example.orderBy("indexNum").asc();
        return skuMapper.selectByExample(example);
    }

    public List<Sku> findByItemIds(Collection<String> itemIds, Integer skuStatus) {
        Example example = getExample();
        example.and().andIn("itemId", itemIds);
        if (skuStatus != null) {
            example.and().andEqualTo("skuStatus", skuStatus);
        }
        return skuMapper.selectByExample(example);
    }

    public Sku findByBarCode(String barCode) {
        Sku sku = new Sku();
        sku.setBarcode(barCode);
        return selectOne(sku);
    }

    public List<Sku> findByBarCodes(List<String> barCodes) {
        Example example = getExample();
        example.and().andIn("barcode", barCodes);
        return baseMapper.selectByExample(example);
    }

    public void deleteByItemId(String itemId) {
        Example example = getExample();
        example.and().andEqualTo("itemId", itemId);
        Sku sku = new Sku();
        sku.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        sku.setGmtModified(Instant.now());
        skuMapper.updateByExampleSelective(sku, example);
    }

    public void updateByBatch(List<SkuBo> skuBos) {
        skuMapper.updateByBatch(skuBos);
    }

    /**
     * 批量更新佣金
     *
     * @param skuBos
     */
    public void updateCommission(List<SkuBo> skuBos) {
        skuMapper.updateCommission(skuBos);
    }

    public void updateStockAndPrice(List<SkuBo> skuBos) {
        skuMapper.updateStockAndPrice(skuBos);
    }

    public int updateFactoryPrice(List<SkuBo> skuBos) {
        return skuMapper.updateFactoryPrice(skuBos);
    }

    public List<Sku> queryItemsSku(List<String> itemIds) {
        Example example = getExample();
        example.and().andIn("itemId", itemIds);
        return baseMapper.selectByExample(example);
    }

    public List<Sku> querySkuList(ItemListApiBo bo) {
/*        Example example = getExample();
        example.and().andEqualTo("itemId",bo.getItemId());
        if (bo.getOrderByModify()!=null&&bo.getOrderByModify()){
            example.setOrderByClause("gmt_modified desc");
        }else {
            example.setOrderByClause("gmt_created desc");
        }*/
        return baseMapper.querySkuList(bo);
    }

    public void updateRealStock(ItemListApiBo bo) {
        skuMapper.updateRealStock(bo);
    }

    public List<Sku> getByBarCodeList(List<String> codeList) {
        Example example = getExample();
        example.and().andIn("barcode", codeList);
        return skuMapper.selectByExample(example);
    }

    public List<SkuStockBo> countStockByItemIds(List<String> itemIds) {
        return skuMapper.countStockByItemIds(itemIds);
    }

    public void updateByItemId(String itemId, Integer stock, BigDecimal retailPrice, BigDecimal itemCommission) {
        Sku sku = new Sku();
        sku.setStock(stock);
        sku.setRetailPrice(retailPrice);
        sku.setItemCommission(itemCommission);
        Example example = getExample();
        example.and().andEqualTo("itemId", itemId);
        skuMapper.updateByExampleSelective(sku, example);
    }
}
